package common

import (
	"net/http"
	"raymond/result"
	"raymond/service"

	"github.com/labstack/echo/v4"
)

type GenerateWalletV1VO struct {
	Id          int64  `json:"id,omitempty"`
	Pr          string `json:"pr,omitempty"`
	Mnemonics   string `json:"mnemonics,omitempty"`
	EthAddress  string `json:"eth_address,omitempty"`
	TronAddress string `json:"tron_address,omitempty"`
}

func GenerateWalletV1API(c echo.Context) error {
	vo := new(GenerateWalletV1VO)
	if err := c.Bind(vo); err != nil {
		return err
	}
	wallet, err := service.GenerateWalletV1()
	if err != nil {
		return err
	}
	id, err := service.SaveWallet(wallet)
	if err != nil {
		return err
	}

	vo.Id = id
	vo.Pr = wallet.PrivateKeyHex
	vo.Mnemonics = wallet.Mnemonics
	vo.EthAddress = wallet.ETHAddress
	vo.TronAddress = wallet.TronAddress

	return c.JSON(
		http.StatusOK,
		result.Ok(vo),
	)
}
